package com.example.tourstor.myself;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.tourstor.BaseBean;
import com.example.tourstor.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by ASUS on 2018/9/14.
 */

public class CardAdapter extends RecyclerView.Adapter<CardAdapter.AuthorViewHolder> {

    private Context mContext;
    private LayoutInflater mInflater;
    private List<BaseBean> mBaseBeans;
    private OnItemClickListener mOnItemClickListener;

    public CardAdapter(Context context) {
        mContext = context;
        mInflater = LayoutInflater.from(context);
        this.mBaseBeans = mBaseBeans;
    }

    //在activity中拿到数据添加进adapter中
    public void notifyAdapter(List<BaseBean> mBaseBeans, boolean isAdd) {
        if (!isAdd) {
            this.mBaseBeans = mBaseBeans;
        } else {
            this.mBaseBeans.addAll(mBaseBeans);
        }
        notifyDataSetChanged();
    }

    //判空
    public List<BaseBean> getMyBean() {
        if (mBaseBeans == null) {
            mBaseBeans = new ArrayList<>();
        }
        return mBaseBeans;
    }

    @Override
    public AuthorViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        return new AuthorViewHolder(mInflater.inflate(R.layout.item_creditcard, parent, false));
    }

    @Override
    public void onBindViewHolder(final AuthorViewHolder holder, int position) {

        final BaseBean myBean = mBaseBeans.get(holder.getAdapterPosition());

        holder.mCardNum.setText(myBean.getCardNum());

        //点击
        (holder.itemView).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnItemClickListener.onItemClick(holder.itemView, holder.getLayoutPosition());
            }
        });

    }

    //声明给外界的方法
    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int pos);
    }


    @Override
    public int getItemCount() {
        return null != mBaseBeans ? mBaseBeans.size() : 0;
    }

    class AuthorViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.card_root_view)
        LinearLayout mCardRootView;

        @BindView(R.id.card_num)
        TextView mCardNum;


        public AuthorViewHolder(View itemView) {
            super(itemView);

            ButterKnife.bind(this, itemView);
        }
    }
}
